package com.example.administrator.personalmail.Activity;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.administrator.personalmail.R;
import com.example.administrator.personalmail.api.Contants;
import com.example.administrator.personalmail.widget.ClearEditText;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;

public class RegSecondActivity extends AppCompatActivity {
    private Button btnComplete;
    private Button btnRegAgain;
    private ClearEditText edtCheck;
    private boolean isFailed=false;
    private String phone = "18408250587";
    private String passward;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg_second);

        phone = getIntent().getStringExtra("phone");
        passward = getIntent().getStringExtra("passward");

        init();
    }

    private void init() {
        btnComplete = findViewById(R.id.btn_complete);
        btnComplete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                check();
                if (isFailed){
                    showNormalDialog();
                }
            }
        });
        btnRegAgain = findViewById(R.id.btn_reSend);
        btnRegAgain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sendCode("86",phone);
                btnRegAgain.setText("已发送");
                btnRegAgain.setEnabled(false);
                btnRegAgain.setBackgroundColor(getResources().getColor(R.color.gray));
            }
        });
        edtCheck = findViewById(R.id.edittxt_code);
    }

    // 请求验证码，其中country表示国家代码，如“86”；phone表示手机号码，如“13800138000”
    public void sendCode(String country, String phone) {
        // 注册一个事件回调，用于处理发送验证码操作的结果
        SMSSDK.registerEventHandler(new EventHandler() {
            public void afterEvent(int event, int result, Object data) {
                if (result == SMSSDK.RESULT_COMPLETE) {

                } else{

                }

            }
        });
        // 触发操作
        SMSSDK.getVerificationCode(country, phone);
    }

    private boolean check(){
        String ch = edtCheck.getText().toString().trim();
        if (TextUtils.isEmpty(ch)){
            Toast.makeText(RegSecondActivity.this,"请输入验证码",Toast.LENGTH_SHORT).show();
            return false;
        }

        submitCode("86",phone,ch);

        return true;
    }

    public void submitCode(String country, String phone, String code) {
        // 注册一个事件回调，用于处理提交验证码操作的结果
        SMSSDK.registerEventHandler(new EventHandler() {
            public void afterEvent(int event, int result, Object data) {
                if (result == SMSSDK.RESULT_COMPLETE) {
                    setResult(Contants.REQUEST_SUCCESS);
                    RegSecondActivity.this.finish();
                } else{
                    isFailed = true;
                }

            }
        });
        // 触发操作
        SMSSDK.submitVerificationCode(country, phone, code);
    }

    private void showNormalDialog(){
        /* @setIcon 设置对话框图标
         * @setTitle 设置对话框标题
         * @setMessage 设置对话框消息提示
         * setXXX方法返回Dialog对象，因此可以链式设置属性
         */
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(RegSecondActivity.this);
        normalDialog.setTitle("验证码错误,请重新输入");
        normalDialog.setMessage("     ");
        // 显示
        normalDialog.show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //用完回调要注销掉，否则可能会出现内存泄露
        SMSSDK.unregisterAllEventHandler();
    }
}
